#!/usr/bin/env python3


"""
使用mysql_orm执行以下sql语句
1. insert
2. delete
3. update
4. select
"""

import multiprocessing

from mysql_orm import ExecuteSql, ParseSql

class User:

    tb_name = "user"

    def add_user(self, num):
        """
        添加新用户
        """
        user_data = dict(name="molio-test", age=num)
        insert_ret, _ = ExecuteSql().execute_command("insert", self.tb_name,
                user_data)
        print(insert_ret)

    def del_user(self):
        """
        删除用户
        """
        user_data = dict(name="molio-test", age=23)
        del_ret, _ = self.handler.execute_del(self.tb_name, user_data)
        print(del_ret)

    def update_user(self, user_data=dict(name="molio-test", age=1000),
            conds=dict(name="molio", age=23)):
        """
        修改用户
        """
        #  user_data = dict(name="molio-test", age=1000)
        #  conds = dict(name="molio", age=23)
        up_ret, _ = ExecuteSql().execute_command('update', self.tb_name,
                user_data, conds)
        print(up_ret)

    def sel_user(self, user_data=['id', 'name', 'age'], conds="1=1"):
        """
        查找用户
        """
        sel_ret, data = ExecuteSql().execute_command('select', self.tb_name,
                user_data, conds)
        print(sel_ret, data)


def main(user_data, conds):
    """
    多线程实现mysql操作
    """
    m_p = multiprocessing.Pool(10)
    res = []
    for i in range(1000, 6000, 100):
        res.append(m_p.apply_async(User().update_user, args=(user_data, conds)))

    m_p.close()
    m_p.join()



if __name__ == "__main__":
    #  u = User()
    conds = dict(name="molio-test", age=1002)
    user_data = dict(name="molio", age=25)
    #  u.add_user(12)
    #  u.del_user()
    #  u.sel_user(conds=conds)
    #  u.update_user(user_data=user_data, conds=conds)
    #  u.sel_user(conds=user_data)
    main(user_data, conds)
